package com.ellecity06.common.widgets.dialog

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.ellecity06.common.base.BaseBottomDialogFragment
import com.ellecity06.common.databinding.DialogBottomDatePickerBinding
import com.ellecity06.common.ktx.todayStr
import com.ellecity06.common.utils.StringUtils
import com.ellecity06.common.widgets.picker.DateTimeConfig.DAY
import com.ellecity06.common.widgets.picker.DateTimeConfig.HOUR
import com.ellecity06.common.widgets.picker.DateTimeConfig.MIN
import com.ellecity06.common.widgets.picker.DateTimeConfig.MONTH
import com.ellecity06.common.widgets.picker.DateTimeConfig.SECOND
import com.ellecity06.common.widgets.picker.DateTimeConfig.YEAR

/**
 * @author ellecity06
 * @time 2025/3/21 14:54
 * @des
 */
/**
 * @param  showType 0 是年月日  1- 年月 ， 2- 年月日时分秒
 */
fun Fragment.showDatePicker(showType: Int = 0, block: (Long) -> Unit) {
    DatePickerDialog(showType, block).show(childFragmentManager, "DatePickerDialog")
}
/**
 * @param  showType 0 是年月日  1- 年月 ， 2- 年月日时分秒
 */
class DatePickerDialog(val showType: Int = 0, val block: (Long) -> Unit) : BaseBottomDialogFragment<DialogBottomDatePickerBinding>() {
    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): DialogBottomDatePickerBinding {
        return DialogBottomDatePickerBinding.inflate(inflater, container, false)
    }

    private val showType0: IntArray by lazy { intArrayOf(YEAR, MONTH, DAY) }
    private val showType1: IntArray by lazy { intArrayOf(YEAR, MONTH) }
    private val showType2: IntArray by lazy { intArrayOf(YEAR, MONTH, DAY, HOUR, MIN, SECOND) }
    private var millisecond: Long = System.currentTimeMillis()
    override fun initialize() {
        binding.datePicker.showLabel(false)
        binding.datePicker.showLabel(false)
        val displayType = when (showType) {
            0 -> showType0
            1 -> showType1
            else -> showType2
        }
        binding.datePicker.setDisplayType(displayType)
        binding.datePicker.setOnDateTimeChangedListener {
            millisecond = it
        }
        binding.tvConfirm.setOnClickListener {
            dismiss()
            block.invoke(millisecond)
        }
    }
}